package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.MemberDao;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ReportDao;
import com.itheima.dao.SetmealDao;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import service.ReportService;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.*;


@Transactional
@Service(interfaceClass = ReportService.class)
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportDao reportDao;

    @Autowired
    private MemberDao memberDao;

    @Autowired
    private OrderDao OrderDao;

    @Autowired
    private SetmealDao setmealDao;

    //会员数量报表指定时间段展示
    @Override
    public List<Integer> getMemberCountByMonth(List<String> list) {
        List<Integer> data = new ArrayList<>();
        for (String month : list) {
            month=month+"-31";
            Integer i = memberDao.findMemberCountBeforeDate(month);
            data.add(i);
        }
        return data;
    }


    //根据月份统计会员数量
    @Override
    public List<Integer> findMemberCountByregTime(ArrayList<String> datalist) {
        //遍历日期集合,查询每个月的会员数量
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        HashMap<String,String> hm = new HashMap();
        ArrayList<Integer> memberCount = new ArrayList();
        for (String data : datalist) {
           String startdata=data+"-1";
           String enddata=data+"+31";//格式：2019-04-31
            hm.put("startdata",startdata );
            hm.put("enddata",enddata );
            Integer count =reportDao.findMemberCountByregTime(hm);
            memberCount.add(count);
        }


        return memberCount;
    }

    @Override
    public List<Map<String, Object>> findSetmealCount() {
        List<Map<String, Object>> list= reportDao.findSetmealCount();
        return list;
    }

    @Override
    public HashMap<String, Object> getBusinessReportData() throws Exception {

        String day = DateUtils.parseDate2String(new Date());  //当前的日期2019-09-11

        Date thisWeekMondayOri = DateUtils.getThisWeekMonday();
        String thisWeekMonday = DateUtils.parseDate2String(thisWeekMondayOri);//本周一的日期2019-09-09

        String mouthOri = day.substring(0, day.lastIndexOf("-")); //当前的月份2019-09
        String mouth= mouthOri+"-01";
        //当天新增会员数
        Integer todayNewMember= memberDao.getCountByday(day);
        //总会员数
        Integer totalMember= memberDao.getCountByday(null);
        //本周新增会员数
        Integer thisWeekNewMember= memberDao.getCountByday(thisWeekMonday);
        //本月新增会员数
        Integer thisMonthNewMember= memberDao.getCountByday(mouth);

        //今日预约数
        Integer todayOrderNumber= OrderDao.getCountByday(day);
        //今日到诊数
        Integer todayVisitsNumber= OrderDao.getCountBydayAndorderType();
        //本周预约数
        Integer thisWeekOrderNumber= OrderDao.getCountByday(thisWeekMonday);
        //本周到诊数
        Integer thisWeekVisitsNumber= OrderDao.getCountBydayAndorderType();
        //本月预约数
        Integer thisMonthOrderNumber= OrderDao.getCountByday(mouth);
        //本月到诊数
        Integer thisMonthVisitsNumber= OrderDao.getCountBydayAndorderType();

        //热门套餐
        List<Map<String,Object>> hotSetmeal=setmealDao.getPopularMeal();
        
        HashMap<String,Object> hm = new HashMap();

        hm.put("reportDate", day);
        hm.put("todayNewMember", todayNewMember);
        hm.put("totalMember", totalMember);
        hm.put("thisWeekNewMember", thisWeekNewMember);
        hm.put("thisMonthNewMember", thisMonthNewMember);
        hm.put("todayOrderNumber", todayOrderNumber);
        hm.put("todayVisitsNumber", todayVisitsNumber);
        hm.put("thisWeekOrderNumber", thisWeekOrderNumber);
        hm.put("thisWeekVisitsNumber", thisWeekVisitsNumber);
        hm.put("thisMonthOrderNumber", thisMonthOrderNumber);
        hm.put("thisMonthVisitsNumber", thisMonthVisitsNumber);
        hm.put("hotSetmeal", hotSetmeal);

        return hm;
    }


    /**
     * @Description: 获取性别占比数据
     * @Param:
     * @return:
     * @Date: 15:04 2019/9/15
     */
    @Override
    public List getSexRatePieReportData() {
        List list = new ArrayList<>();
        List manList = new ArrayList<>();
        List womanList = new ArrayList<>();

        List<Map> Ratelist = reportDao.querySexRateNums();


        for (Map map : Ratelist) {
            if ("1".equals(map.get("sex"))) {
                //Integer sex = (Integer) map.get("sex");
                BigDecimal bigDecimal = (BigDecimal) map.get("rate");
                double rate = bigDecimal.doubleValue();
                manList.add("男性会员");
                manList.add(rate);
            } else {
                BigDecimal bigDecimal = (BigDecimal) map.get("rate");
                double rate = bigDecimal.doubleValue();
                womanList.add("女性会员");
                womanList.add(rate);
            }
        }
        list.add(manList);
        list.add(womanList);
        return list;
    }

    /**
     * @Description: 获取不同年龄会员占比数据
     * @Param:
     * @return:
     * @Date: 15:04 2019/9/15
     */
    @Override
    public List getAgeRatePieReportData() {
        //设置年龄段端点
        int age1 = 20;
        int age2 = 40;
        int age3 = 60;
        Map paraMap = null;
        BigDecimal bigDecimal = null;
        //计算年龄段端点的生日
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        month = month + 1;
        int date = calendar.get(Calendar.DAY_OF_MONTH);

        //年龄段一的终端
        String age1Bir = (year - age1) + "-" + month + "-" + date;
        //年龄段二的终端
        String age2Bir = (year - age2) + "-" + month + "-" + date;
        //年龄段三的终端
        String age3Bir = (year - age3) + "-" + month + "-" + date;


        //获取年龄段一的数据
        paraMap = new HashMap<>();
        paraMap.put("ageStart", null);
        paraMap.put("ageEnd", age1Bir);
        Double group1Rate = reportDao.queryAgeGroupNum(paraMap);
        //bigDecimal = new BigDecimal(group1Rate);
        //group1Rate = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        //获取年龄段二的数据
        paraMap = new HashMap<>();
        paraMap.put("ageStart", age1Bir);
        paraMap.put("ageEnd", age2Bir);
        Double group2Rate = reportDao.queryAgeGroupNum(paraMap);
        //bigDecimal = new BigDecimal(group2Rate);
        //group2Rate = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        //获取年龄段三的数据
        paraMap = new HashMap<>();
        paraMap.put("ageStart", age2Bir);
        paraMap.put("ageEnd", age3Bir);
        Double group3Rate = reportDao.queryAgeGroupNum(paraMap);
        //bigDecimal = new BigDecimal(group3Rate);
        //group3Rate = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        //获取年龄段四的数据
        paraMap = new HashMap<>();
        paraMap.put("ageStart", age3Bir);
        paraMap.put("ageEnd", null);
        Double group4Rate = reportDao.queryAgeGroupNum(paraMap);
        //bigDecimal = new BigDecimal(group4Rate);
        // group4Rate = bigDecimal.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();

        List list = new ArrayList<>();
        List groupList2 = new ArrayList<>();
        List groupList3 = new ArrayList<>();
        List groupList4 = new ArrayList<>();
        Map map1 = new HashMap<>();

        map1.put("name", "20岁以下会员");
        map1.put("y", group1Rate);
        map1.put("sliced", "true");
        map1.put("selected", "true");

        groupList2.add("20-40岁会员");
        groupList2.add(group2Rate);
        groupList3.add("40-60岁会员");
        groupList3.add(group3Rate);
        groupList4.add("60岁以上会员");
        groupList4.add(group4Rate);

        list.add(map1);
        list.add(groupList2);
        list.add(groupList3);
        list.add(groupList4);
        return list;
    }
}
